﻿using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.Runtime.Serialization.Formatters.Binary;

namespace Vision.Core.Components
{
    /// <summary>
    /// 
    /// </summary>
    public static class Common
    {
        /// <summary>
        /// 
        /// </summary>
        public static class Serialization
        {
            /// <summary>
            /// 
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static string SerializeObjectToString<T>(T obj)
            {
                string returnString = string.Empty;
                using (MemoryStream mStream = new MemoryStream()){
                    BinaryFormatter binaryFormatter = new BinaryFormatter();
                    binaryFormatter.Serialize(mStream, obj);
                    returnString = Convert.ToBase64String(mStream.ToArray());
                }
                return returnString;                            
            }

            /// <summary>
            /// 
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static T DeserializeObjectFromString<T>(string obj)
            {                
                using (MemoryStream mStream = new MemoryStream(Convert.FromBase64String(obj))){
                    BinaryFormatter binaryFormatter = new BinaryFormatter();
                    return (T)binaryFormatter.Deserialize(mStream);                                        
                }
            }

            /// <summary>
            /// 
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="obj"></param>
            /// <returns></returns>
            public static XmlDocument SerializeObjectToXml<T>(T obj)
            {
                XmlDocument xmlDoc = new XmlDocument();
                using (MemoryStream mStream = new MemoryStream()){
                    XmlSerializer serializer = new XmlSerializer(typeof(T));
                    serializer.Serialize(mStream, obj);
                    mStream.Seek(0, 0);
                    xmlDoc.Load(mStream);
                }
                return xmlDoc;
            }

            /// <summary>
            /// 
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="reader"></param>
            /// <returns></returns>
            public static T DeserializeObjectFromXml<T>(XmlReader reader)
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                if (serializer.CanDeserialize(reader)){
                    return (T)serializer.Deserialize(reader);
                }
                else{
                    return default(T);
                }
            }

            /// <summary>
            /// 
            /// </summary>
            /// <param name="doc"></param>
            /// <returns></returns>
            public static System.Xml.Linq.XElement CreateXElementFromDoc(System.Xml.XmlDocument doc)
            {
                using (MemoryStream mStream = new MemoryStream()){
                    doc.Save(mStream);
                    mStream.Seek(0, 0);
                    return System.Xml.Linq.XElement.Load(mStream);
                }
            }

            /// <summary>
            /// 
            /// </summary>
            public class InsideCOMBinder : System.Runtime.Serialization.SerializationBinder
            {
                /// <summary>
                /// 
                /// </summary>
                /// <param name="assemblyName"></param>
                /// <param name="typeName"></param>
                /// <returns></returns>
                public override Type BindToType(string assemblyName, string typeName){
                    return Type.GetType(String.Format("{0}, {1}", typeName, assemblyName));
                }

            }
        }                
    
        
    }
}
